//
//  PropertiesViewController.m
//  smarthings
//
//  Created by Stefan Tsvyatkov on 10/25/14.
//  Copyright (c) 2014 Stefan Tsvyatkov. All rights reserved.
//

#import "PropertiesActionsViewController.h"
#import "PropertyDetailsViewController.h"
#import "ActionDetailsViewController.h"

@interface PropertiesActionsViewController ()

@property (nonatomic, strong) NSMutableArray * items;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *addButton;

@end

@implementation PropertiesActionsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.dataType == LDTProperties) {
        if (self.thing.properties.count != self.items.count) {
            self.thing.properties = [NSArray arrayWithArray:self.items];
        }
    }
    if (self.dataType == LDTActions) {
        if (self.thing.actions.count != self.items.count) {
            self.thing.actions = [NSArray arrayWithArray:self.items];
        }
    }
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.dataType == LDTActions) {
        self.items = [NSMutableArray arrayWithArray:self.thing.actions];
    }
    else if (self.dataType == LDTProperties) {
        self.items = [NSMutableArray arrayWithArray:self.thing.properties];
    }
    if (!self.isNewThing) {
        self.navigationItem.rightBarButtonItem = nil;
    }
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.items.count;
    }
    else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellIdentifier = nil;
    if (self.dataType == LDTProperties) {
        cellIdentifier = @"PropertyCell";
    }
    else if (self.dataType == LDTActions) {
        cellIdentifier = @"ActionCell";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [(SMTGeneralThingsObject *)(self.items[indexPath.row]) name];
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.isNewThing;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.items removeObject:self.items[indexPath.row]];
        [self.tableView reloadData];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ViewProperty"]) {
        [(PropertyDetailsViewController *)[segue destinationViewController] setProperty:[self.items objectAtIndex:self.tableView.indexPathForSelectedRow.row]];
        [(PropertyDetailsViewController *)[segue destinationViewController] setIsNewThing:NO];
        [[(PropertyDetailsViewController *)[segue destinationViewController] navigationItem] setTitle:[[self.items objectAtIndex:self.tableView.indexPathForSelectedRow.row] name]];
    }
    else if ([segue.identifier isEqualToString:@"AddProperty"]) {
        SMTThingProperty *property = [[SMTThingProperty alloc] init];
        property.propertyType = [Smarthings thingPropertyTypes][0];
        property.name = property.propertyType.uuid;
        [self.items addObject:property];
        [(PropertyDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setProperty:property];
        [(PropertyDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setPropertiesArray:self.items];
        [(PropertyDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setIsNewThing:YES];
    }
    else if ([segue.identifier isEqualToString:@"ViewAction"]) {
        [(ActionDetailsViewController *)[segue destinationViewController] setAction:[self.items objectAtIndex:self.tableView.indexPathForSelectedRow.row]];
        [(ActionDetailsViewController *)[segue destinationViewController] setIsNewThing:NO];
        [[(ActionDetailsViewController *)[segue destinationViewController] navigationItem] setTitle:[[self.items objectAtIndex:self.tableView.indexPathForSelectedRow.row] name]];
    }
    else if ([segue.identifier isEqualToString:@"AddAction"]) {
        SMTThingAction *action = [[SMTThingAction alloc] init];
        action.actionType = [Smarthings thingActionTypes][0];
        action.name = action.actionType.uuid;
        [self.items addObject:action];
        [(ActionDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setAction:action];
        [(ActionDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setActionsArray:self.items];
        [(ActionDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setIsNewThing:YES];
    }
    
}

@end
